package com.easytouch.vmhdl;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.baidu.bpit.facepay.face.FacePaySDKManager;
import com.easytouch.core.Configure;
import com.easytouch.core.utils.CrashHandler;
import com.easytouch.core.utils.Log;
import com.easytouch.core.utils.SDCardTools;
import com.easytouch.core.utils.SystemTools;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * Created by gulin on 2017/11/30.
 */

public class AppApplication extends Application {
    public static Context context;
    public static String versionName;
    public static String path;
    public static String ext_path;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

        FacePaySDKManager.getInstance().initialize(context, "fengguo-face-android", "idl-license.face-android");
        versionName = getVersionName();
        path = SDCardTools.getSDCardPath() + "/Android/data/" + getPackageName();
        ext_path = "/mnt/extsd/"+ this.getPackageName();
        initImageLoader(this.getApplicationContext());

        /**
         * 开始初始化驱动
         */
        //初始化crashhandler
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init();
        //初始化Log
        Log.maintainDays = 5;
        Log.isInfoEnable = true;
        Log.isDebugEnable = true;
        Log.isErrorEnable = true;
        //初始化Configure
        Configure.context = context;
        Configure.path = path;
        Configure.version = versionName;
        //检查系统是否root
        SystemTools.initSystemDoor();
    }

    public String getVersionName() {
        PackageManager pm = this.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(this.getPackageName(), 0);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void initImageLoader(Context context) {

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileCount(500)
                .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();

        ImageLoader.getInstance().init(config);
    }
}
